using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace GPUAPP2
{
    public class DataTransformer
    {
        public class TransformDataRow
        {
            public int RowNumber { get; set; }
            public int Value1 { get; set; }
            public int Value2 { get; set; }
            public int Value3 { get; set; }
            public int[] SortedValues { get; set; }
            public string SortedString { get; set; }
            public int Count { get; set; }  // 添加命中次数字段
        }

        public TransformDataRow[] LoadTransformData(string filePath)
        {
            try
            {
                var lines = File.ReadAllLines(filePath);
                var data = new TransformDataRow[lines.Length];

                for (int i = 0; i < lines.Length; i++)
                {
                    // 分割行数据，移除空格和星号
                    var values = lines[i]
                        .Split(new[] { ',', ' ','-' }, StringSplitOptions.RemoveEmptyEntries)
                        .Where(v => !v.Contains("*") && !v.Contains("'"))
                        .Select(int.Parse)
                        .ToArray();

                    if (values.Length >= 3)
                    {
                        data[i] = new TransformDataRow
                        {
                            RowNumber = i + 1,
                            Value1 = values[0],
                            Value2 = values[1],
                            Value3 = values[2]
                        };
                    }
                    else
                    {
                        throw new Exception($"第{i + 1}行数据格式不正确，需要3个数字");
                    }
                }

                return data;
            }
            catch (Exception ex)
            {
                throw new Exception($"加载转换前数据失败: {ex.Message}");
            }
        }

        public List<string> TransformData(TransformDataRow[] transformData, int[] N)
        {
            List<string> sortedStrings = new List<string>(transformData.Length); // 预分配容量

            for (int i = 0; i < transformData.Length; i++)
            {
                var row = transformData[i];
                
                // 直接读取三个值并手动排序
                int a = N[row.Value1], b = N[row.Value2], c = N[row.Value3];
                
                // 手动排序三个元素（避免Array.Sort）
                if (a > b) { int tmp = a; a = b; b = tmp; }
                if (b > c) { int tmp = b; b = c; c = tmp; }
                if (a > b) { int tmp = a; a = b; b = tmp; }

                // 直接拼接字符串（避免中间数组）
                sortedStrings.Add($"{a}{b}{c}");
            }

            return sortedStrings;
        }
    }
} 